$(function(){
	//获取本地离线存储
	function eachList(){
		
	var local=JSON.parse(localStorage.getItem("storeInfo"));
	$.each(local, function(i,item) {
		var  list=`<li>
							<span>
								<span id="box_`+[i]+`"></span>
								<img src=`+local[i].img+`  />
							</span>
							<span>
								<i>`+local[i].storeName+`</i>
								<b></b>
								<select>
									<option>购买延保服务</option>
									<option>延长保修2年</option>
									<option>延长保修3年</option>
									<option>延长保修3年</option>
								</select>
							</span>
							<span>
								<div><em>￥ </em><b>`+local[i].rprice+`</b></div>
								<div><em>￥ </em><b id="price_`+[i]+`">`+local[i].price+`</b></div>
								<div><b>大聚惠</b></div>
							</span>
							<span>
								<span class="lessen">-</span>
								<input type="text" value=`+local[i].num+` id="num_`+[i]+`"/>
								<span class="add">+</span>
							</span>
							<span>
								<em>¥</em><b id="sum_`+[i]+`">`+local[i].sum+`</b>
							</span>
							<span>
								<a href="javascript:void(0)" class="collect_`+[i]+`">移入收藏夹</a>
								<a href="javascript:void(0)" class="remove">删除</a>
							</span>
				</li>`

		$("#list").append(list);
	});
	}
	eachList();
	
	
	var arr=[];
	var total=0;
	var a=0;
	
	//删除操作
	function Remove(_this)
	{
		var index=_this.parents("li").index();
		_this.parents("li").remove();
		var local=JSON.parse(localStorage.getItem("storeInfo"));
		local.splice(index,1);
		localStorage.setItem("storeInfo",JSON.stringify(local));
	}

	//移动收藏夹
	function Collect()
	{
		alert("添加成功")
	}
	
	
	//判断是否勾选上
	function isCheck(_this)
	{
		
		var box=_this.parents("li").find("span[id^='box_']");
		if(box.attr("class")=="")
		{
			
			return false;
		}else if(box.attr("class")=="on")
		{
		
			return true;	
		}
		
	}
	//小计
	function Sum(_this){
		var num=Number(_this.parents("li").find("input[id^='num_']").val());
		var price=Number(_this.parents("li").find("b[id^='price_']").html());
		var sum=_this.parents("li").find("b[id^='sum_']");
		sum.html((num * price).toFixed(2));
		
	}
	//总计
	function Total(){
		
		total=0;
		
		$("span[id^='box_']").each(function(){
			if($(this).attr("class")=="on")
			{
				arr.push(Number($(this).parents("li").find("b[id^='sum_']").html()));
			}
		})
		$.each(arr, function(i,item) {
				total+=item;
		});
		$("#total").html(total);
		//设置勾选商品的件数
		$("#count").html(arr.length);
	}
	//数量操作判断
	function  changeNum(_this)
	{
		var num=Number(_this.val());
			if(isNaN(num)||num<1)
			{
				_this.val(1);
			}else{
				_this.val(parseInt(num));
			}
	}
	//减少num操作
	$(".lessen").click(function(){
		arr=[];
		var num=Number($(this).parents("li").find("input[id^='num_']").val());
		
		if(isNaN(num) || num<2)
		{
			$(this).parents("li").find("input[id^='num_']").val(1);
		}else{
			num=parseInt(num);
			num-=1;
			$(this).parents("li").find("input[id^='num_']").val(num);
		}
		if(isCheck($(this)))
		{
			Sum($(this));
			Total();
		}
	})
	//增加num操作
	$(".add").click(function(){
		arr=[];
		var num=Number($(this).parents("li").find("input[id^='num_']").val());
		
		if(isNaN(num))
		{
			$(this).parents("li").find("input[id^='num_']").val(1);
		}
		if(num>9998)
		{
			$(this).parents("li").find("input[id^='num_']").val(9999);
		}else{
			num=parseInt(num);
			num+=1;
			$(this).parents("li").find("input[id^='num_']").val(num);
		}
		if(isCheck($(this)))
		{
			Sum($(this));
			Total();
		}
	})
	//输入框失去焦点
	$("input[id^='num_']").blur(function(){
			total=0;
			arr=[];
			changeNum($(this));
			if(isCheck($(this)))
			{
				Sum($(this));
				Total();
			}
		})
	//点击勾选框
	
		$("span[id^='box_']").click(function(){
			arr=[];
			if($(this).attr("class")=="on")
			{
				$(this).attr("class","");
				a--;
			}else{
				$(this).attr("class","on");
				a++;
			}
			
			
			$("span[id='check-all']").attr("class",$("span[id^='box_']").length==a?"on":"");
				if(isCheck($(this)))
				{
					Sum($(this));
					Total();
				}else{
					Total();
				}
			})
	
		
		
		
	//全选功能
		$("span[id=check-all]").click(function(){
			arr=[];
		var check=$("span[id='check-all']").attr("class");
		if(check=="on")
		{
			$("span[id='check-all']").attr("class","");
			$("span[id^='box_']").attr("class","");
			a=0;
			Total();
		}else{
			$("span[id='check-all']").attr("class","on");
			$("span[id^='box_']").attr("class","on");
			a=$("span[id^='box_']").length;
			if(isCheck($(this)))
				{
					Sum($(this));
					Total();
				}else{
					Total();
				}
		}
			
		})
	//点击当前行删除按钮
		
			$(".remove").click(function(){
				
				arr=[];
				Remove($(this));
				
				if(isCheck($(this)))
				{
					Sum($(this));
					Total();
				}else{
					Total();
				}
			})
		
		
	//点击删除按钮
	$("#removeAll").click(function(){
		
		$("span[id^='box_']").each(function(){
			arr=[];
			if($(this).attr("class")=="on")
			{
				Remove($(this));
				Sum($(this));
				Total();
			}
		})
		
	})
	
	//点击收藏夹按钮
	$("#collect").click(function(){
		$("span[id^='box_']").each(function(){
			
			if($(this).attr("class")=="on")
			{
				Collect();
			}
		})
	})
	//点击当前收藏夹按钮
	$("a[class^='collect_']").each(function(){
		$(this).click(function(){
			Collect();
		})
	})
	//点击结算按钮
	$("#total-prices").click(function(){
		var total=$("#total").html();
		var total=Number(total).toFixed(2);
		if(total &&total!=0)
		{
			var price={
			total:total
			}
			setPriceStorage(price);
		}else{
			alert("你还没有任何选择商品");
		}
		
		
	})
	//设置本地存储
	function setPriceStorage(price){
		var priceInfo=[
			price
		];
		localStorage.setItem("total-price",JSON.stringify(priceInfo));
		window.location.href="confirm_order.html";
	}
})
